
var util = require('/utils/util.js');
// var getOpenId = require('/utils/util.js').getOpenId;
// var addUserInfos = require('/utils/util.js').addUserInfos;



App({
  onLaunch: function () {
    this.getUserInfo();

  },
  getUserInfo: function () {
    var that = this
    //调用登录接口
    wx.login({
      success: function (res) {
        console.log("llllllll获取code呀呀呀呀呀");
        console.log(res);
        // 获取openid
        // console.log(getOpenId(res.code))
        var myCode = res.code
       
        wx.getUserInfo({
          
          success: function (res) {
           
            console.log(myCode)
            that.globalData.userInfo = res.userInfo
            console.log("globalData====" + JSON.stringify(that.globalData));


            if (that.globalData.status == null) {
              wx.request({
                url:util.serveUrl + "get_openid_by_code",
                method: "POST",
                header: {
                  "Content-Type": "application/x-www-form-urlencoded",
                },
                data: {
                  code: myCode
                },
                success: function (res) {
                  console.log("我是通过code获取");
                  // 设置缓存
                  try {
                    wx.setStorageSync('userStateAndOpenid', res.data.data)
                  } catch (e) {
                  }
                  console.log("添加用户函数")
                  util.addUserInfos(that.globalData.userInfo, res.data.data.openid)

                }
              })
            } else{
              wx.request({
                url: util.serveUrl + "get_openid_by_status_code",
                method: "POST",
                header: {
                  "Content-Type": "application/x-www-form-urlencoded",
                },
                data: {
                  status_code: that.globalData.status,
                  code: myCode
                },
                success: function (res) {
                  console.log("我是通过 status_code 获取 openid");
                  console.log(res)
                  try {
                    wx.setStorageSync('userStateAndOpenid', res.data.data)
                  } catch (e) {
                  }
                }
              })
              // console.log(that.globalData.status)
            }
          },
          fail: function (err) {
            console.log("点击了拒绝");
            console.log(err)
          }
        })
      }
    });
    
      wx.getStorage({
        key: 'userStateAndOpenid',
        success: function (res) {
          console.log("看看啊坎坎坷坷咳咳咳")
          console.log(res.data.openid)
          that.globalData.opeid = res.data.openid,
          that.globalData.status = res.data.status_code;
          // console.log(that.globalData.status)
        }
      });
    
    
  },
  // globalData:{
  //   userInfo:null
  // },
  globalData: {
    userInfo: null,
    userData: null,
    opeid: null,
    mydata: null,
    myInfos: null,
    userHead: null,
    status:null
  }
})